(function () {
    var oMore = document.querySelector('.school-more').querySelector('a');
    var oSchCon = document.querySelector('.school-con');
    var onOff = true;

    oMore.addEventListener('click', function () {
        if (onOff) {
            this.innerHTML = '点击收缩更多';
            this.className = 'active';
            oSchCon.style.height = 'auto';
        } else {
            this.innerHTML = '点击查看更多';
            this.className = '';
            oSchCon.style.height = 'calc(12rem + 3px)';
        }
        onOff = !onOff;
    }, false);
})();

(function () {
    var aCon = document.querySelectorAll('.list-container');
    for(var i = 0, length1 = aCon.length; i < length1; i++){
        aCon[i].onOff = true;
        aCon[i].count = 0;
        var oMore = aCon[i].querySelector('.list-more').querySelector('a');
        oMore.addEventListener('click', selectMore, false);
    }

    function selectMore() {
        var dad = this.parentNode.parentNode;
        if (dad.onOff) {
            showMore.call(this);
        } else {
            hideMore.call(this);
        }
    }

    function showMore() {
        this.innerHTML = '点击收缩更多';
        this.className = 'active';
        var dad = this.parentNode.parentNode;
        var oUl = dad.querySelector('ul');
        dad.onOff = false;
        oUl.style.height = 'auto';
        //获取省ID
        var province_id=this.getAttribute('province_id');
//      alert(province_id);
        if (dad.count == 0) {
            dad.count++;
            ajax({
                method: 'post',
                url: './getMoreSchool',
                data: 'id='+province_id,
                success: function (data) {
                	oUl.innerHTML='';
                    var data = JSON.parse(data);
                    for(var i = 0, length1 = data.length; i < length1; i++){
                        var str = '<a href="'+ data[i]['url'] +'">'+ data[i]['title'] +'</a>';
                        var oLi = document.createElement('li');
                        oLi.innerHTML = str;
                        if (data[i]['hot']) {
                            oLi.className = 'hot';
                        }
                        oUl.appendChild(oLi);
                    }
                }
            })
        }
    }

    function hideMore() {
        this.innerHTML = '点击展开更多';
        this.className = '';
        var dad = this.parentNode.parentNode;
        var oUl = dad.querySelector('ul');
        dad.onOff = true;
        oUl.style.height = '8.0rem';
    }

    function ajax(jax) {
        var method = jax.method || 'get';
        var url = jax.url;
        var data = jax.data;
        var success = jax.success;
        var xhr = null;

        try {
            xhr = new XMLHttpRequest();
        } catch(e) {
            xhr = new ActiveXObject('Microsort.XMLHTTP');
        }

        if (method == 'get' && data) {
            url += '?' + data;
        }

        xhr.open(method, url, true);

        if (method == 'get') {
            xhr.send();
        } else {
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send(data);
        }
        
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    success && success(xhr.responseText);
                } else {
                    console.log("Error: " + xhr.status);
                }
            }
        }
    }
})();